package org.pjlib.common;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;

import org.pjlib.R;

/**
 * Created by ws on 2019/05/24.
 */
public class AlertDialog extends BaseDialog {
    public AlertDialog(Context context) {
        super(context, R.style.alert_dialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    public TextView getMsgView(){
        return findViewById(R.id.tv_msg);
    }
    public static class Builder {
        private Context ctx;
        private AlertDialog dialog;
        private int icon;
        private String singleStr;
        private OnClickListener singleListener;
        private String title;
        private CharSequence message;
        private boolean cancelable=true;
        private boolean canceledOnTouchOutside=true;
        private String negativeStr;
        private OnClickListener negativeListener;
        private String positiveStr;
        private OnClickListener positiveListener;

        public Builder(Context ctx) {
            this.ctx = ctx;
        }

        public Builder setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
            this.canceledOnTouchOutside = canceledOnTouchOutside;
            return this;
        }

        public Builder setIcon(int icon) {
            this.icon = icon;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setMessage(CharSequence message) {
            this.message = message;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public Builder setNegativeButton(String str, OnClickListener clickListener) {
            negativeStr = str;
            negativeListener = clickListener;
            return this;
        }

        public Builder setPositiveButton(String str, OnClickListener clickListener) {
            positiveStr = str;
            positiveListener = clickListener;
            return this;
        }

        public Builder setSingleButton(String str, OnClickListener clickListener) {
            singleStr = str;
            singleListener = clickListener;
            return this;
        }

        public AlertDialog builder() {
            dialog = new AlertDialog(ctx);
            View rootView = LayoutInflater.from(ctx).inflate(R.layout.dialog_alert, null);
            dialog.setContentView(rootView);
            BViewHolder holder = new BViewHolder(rootView);
            holder.clicked(R.id.tv_quit, btnClick)
                    .clicked(R.id.tv_ok, btnClick)
                    .clicked(R.id.tv_single_ok, btnClick);
            holder.text(R.id.tv_title, title)
                    .text(R.id.tv_msg, message)
                    .text(R.id.tv_quit, StringUtils.isEmpty( negativeStr)?"取消":negativeStr)
                    .text(R.id.tv_ok, StringUtils.isEmpty(positiveStr)?"确定":positiveStr)
                    .text(R.id.tv_single_ok, StringUtils.isEmpty(singleStr)?"确定":singleStr);
            if(message instanceof SpannableString || message instanceof SpannableStringBuilder){
                TextView text=holder.findViewById(R.id.tv_msg);
                text.setMovementMethod(LinkMovementMethod.getInstance());
                text.setHighlightColor(Color.TRANSPARENT);
            }
            dialog.setCancelable(cancelable);
            dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
            if (singleListener != null) {
                holder.gone(R.id.ll_bottom_btn)
                        .visible(R.id.tv_single_ok);
                dialog.setCancelable(false);
                dialog.setCanceledOnTouchOutside(false);
            }
            if(icon>0){
                holder.image(R.id.iv_icon,icon)
                        .visible(R.id.iv_icon);
            }

            return dialog;
        }

        private View.OnClickListener btnClick = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null) {
                    dialog.dismiss();
                }
                int i = v.getId();
                if (i == R.id.tv_quit) {
                    if (negativeListener != null) {
                        negativeListener.onClick(dialog, 0);
                    }
                } else if (i == R.id.tv_ok) {
                    if (positiveListener != null) {
                        positiveListener.onClick(dialog, 1);
                    }
                } else if (i == R.id.tv_single_ok) {
                    if (singleListener != null) {
                        singleListener.onClick(dialog, 2);
                    }
                }
            }
        };

        public void show() {
            builder().show();
        }
    }
}
